package cc.rengu.igas.opms.core.service.trans.common;

import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.core.service.base.ManageService;
import cc.rengu.igas.opms.facade.request.GetUserAuthInfoRequest;
import cc.rengu.igas.opms.facade.response.GetUserAuthInfoResponse;
import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.oltp.service.base.ChannelBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.enums.CheckResultEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.realize.TransCustomImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.GlobalTransUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 获取用户授权信息
 * modify by wangcheng 2021-09-13
 */
public class GetUserAuthInfoService extends ManageService implements TransCustomImpl {
    @Override
    protected Object bizBeforeCust() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        GetUserAuthInfoRequest request = new GetUserAuthInfoRequest();
        // 读取存在xml树里的实体类
        ConvertUtil.convertOutput(request);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM, request.getHeader().getTraceNo());
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        GetUserAuthInfoRequest getUserAuthInfoRequest = (GetUserAuthInfoRequest) request;
        GetUserAuthInfoResponse getUserAuthInfoResponse = new GetUserAuthInfoResponse();
        getUserAuthInfoResponse.setHeader(getUserAuthInfoRequest.getHeader());

        //调用通道接口
        if (null == txnInfo.getChannelCallCfgList() || txnInfo.getChannelCallCfgList().isEmpty()) {
            rglog.error("<{}-{}>交易未配置交易通道服务调用配置信息！", txnInfo.getSrcTxnAuthCfg().getTxnNum(), txnInfo.getSrcTxnAuthCfg().getTxnName());
            throw new BizException(RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespCode(), RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespDesc());
        }
        List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item ->
                item.getCallIndex().equals(ChannelBaseService.FIRST_CHANNEL_CALL_INDEX)).collect(Collectors.toList());
        ChannelCallCfg channelCallCfg = channelCallSceneDiscern(getUserAuthInfoRequest, channelCallCfgList);
        Class channelClasz = Class.forName(channelCallCfg.getCallChannelImpl());
        ChannelBaseService channelBaseService = (ChannelBaseService) channelClasz.newInstance();
        channelBaseService.callService(txnInfo, null, channelCallCfg.getBizScene(), request);

        //获取微信或者支付宝的用户相关信息
        String wxalUserId = (String) GlobalTransUtil.getCacheValue(TransCacheConstant.WXAL_USER_ID);
        getUserAuthInfoResponse.setUserId(wxalUserId);
        getUserAuthInfoResponse.setCountry((String) GlobalTransUtil.getCacheValue(TransCacheConstant.WXAL_COUNTRY));
        getUserAuthInfoResponse.setProvince((String) GlobalTransUtil.getCacheValue(TransCacheConstant.WXAL_PROVVINCE));
        getUserAuthInfoResponse.setCity((String) GlobalTransUtil.getCacheValue(TransCacheConstant.WXAL_CITY));
        getUserAuthInfoResponse.setUserSex((String) GlobalTransUtil.getCacheValue(TransCacheConstant.WXAL_SEX));
        getUserAuthInfoResponse.setNickName((String) GlobalTransUtil.getCacheValue(TransCacheConstant.WXAL_NICK_NAME));
        getUserAuthInfoResponse.setUserImagePath((String) GlobalTransUtil.getCacheValue(TransCacheConstant.WXAL_HEAD_IMAGE));
        getUserAuthInfoResponse.setUserPrivilege((String) GlobalTransUtil.getCacheValue(TransCacheConstant.WXAL_USER_PRIVILEGE));
        getUserAuthInfoResponse.setUnionId((String) GlobalTransUtil.getCacheValue(TransCacheConstant.WXAL_USER_UNIONID));
        //拼装应答报文
        getUserAuthInfoResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        getUserAuthInfoResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        BizResponse<GetUserAuthInfoResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(getUserAuthInfoResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GetUserAuthInfoResponse getUserAuthInfoResponse = (GetUserAuthInfoResponse) bizResponse.getResult();
        ConvertUtil.convertInput(getUserAuthInfoResponse);
    }

    @Override
    public ChannelCallCfg channelCallSceneDiscern(Object request, List<ChannelCallCfg> channelCallCfgList) throws Exception {
        GetUserAuthInfoRequest getUserAuthInfoRequest = (GetUserAuthInfoRequest) request;
        String authChannel = getUserAuthInfoRequest.getAuthChannel();
        String finalBizScene = "ALIPAY".equalsIgnoreCase(authChannel) ? "01" : ShareParamConstant.CALL_CFG_DEFAULT_BIZ_SCENE;
        Optional<ChannelCallCfg> channelCallCfgOpt = channelCallCfgList.stream().filter(item ->
                item.getBizScene().equals(finalBizScene)).findFirst();
        return channelCallCfgOpt.orElse(null);
    }

    @Override
    public CheckResultEnum channelCallPreCheck(Object request, ChannelCallCfg channelCallCfg, OrderInfo orderInfo) throws Exception {
        return CheckResultEnum.PASSED;
    }

    @Override
    public TransStatusEnum channelCallResultProc(Object request, ChannelCallCfg channelCallCfg, TransStatusEnum transStatusEnum, Object channelResponse, OrderInfo needUpdateOrderInfo) throws Exception {
        return transStatusEnum;
    }
}
